﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using TinyMoocSite.DataAccessLayer;
using TinyMoocSite.Models;
using TinyMoocSite.ViewModels;
using TinyMoocSite.Helpers;
using TinyMoocSite.Filters;

namespace TinyMoocSite.Controllers
{
    public class DefaultController : Controller
    {
        // GET: Default
        public ActionResult Index()
        {
            UserInfoHelper helper = new UserInfoHelper();
            UserInfo userInfo = helper.GetUserInfoByUsername(User.Identity.Name);

            if (userInfo != null)
            {
                // Student Controller is deprecated now.
                if (userInfo.Privilege == UserPrivilege.Student)
                    return RedirectToAction("/Index", "Student");
                if (userInfo.Privilege == UserPrivilege.Teacher)
                    return RedirectToAction("/Index", "Teacher");
                if (userInfo.Privilege == UserPrivilege.Administrator)
                    return RedirectToAction("/Index", "Administrator");
            }

            return View();
        }

        public ActionResult Login(LoginForm login)
        {
            UserInfoHelper helper = new UserInfoHelper();
            if (helper.ValidateUserLogin(login.Username, login.Password))
            {
                FormsAuthentication.SetAuthCookie(login.Username, false);
                Session["Role"] = helper.GetUserPrivilege(login.Username).ToString();
                helper.RefreshLoginDate(login.Username);
            }

            return RedirectToAction("/");
        }

        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("/");
        }

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult RegisterNew(RegisterForm register)
        {
            UserInfoHelper helper = new UserInfoHelper();
            if (register.Password == register.ConfirmedPassword && helper.GetUserInfoByUsername(register.Username) == null)
            {
                helper.InsertUserInfo(register);
                return RedirectToAction("/");
            }
            return RedirectToAction("/Register");
        }

        [HeaderFooterFilter]
        public ActionResult UserInfo()
        {
            UserDetailForm userDetail = new UserDetailForm();
            UserInfoHelper helper = new UserInfoHelper();
            UserInfo userInfo = helper.GetUserInfoByUsername(User.Identity.Name);
            if (userInfo != null)
            {
                userDetail.MyFullName = userInfo.FullName;
                userDetail.MyLicenseId = userInfo.LicenseId;
                userDetail.Email = userInfo.Email;
                userDetail.Username = userInfo.Username;
                userDetail.LastActivityDate = userInfo.LastActivityDate.ToLongDateString();
                userDetail.Memo = userInfo.Memo;
                userDetail.Privilege = userInfo.Privilege.ToString();
                userDetail.ProfTitle = userInfo.ProfTitle;
                userDetail.RegisterDate = userInfo.RegisterDate.ToLongDateString();
                userDetail.Website = userInfo.Website;
            }
            return View(userDetail);
        }

        [HttpPost]
        public ActionResult UpdateUserInfo(UserDetailForm userDetail)
        {
            UserInfoHelper helper = new UserInfoHelper();
            helper.UpdateUserInfo(userDetail);
            return RedirectToAction("/");
        }

        [HeaderFooterFilter]
        public ActionResult ChangePassword()
        {
            ChangePasswordForm form = new ChangePasswordForm();
            return View(form);
        }

        [HttpPost]
        public ActionResult DoChangePassword(ChangePasswordForm form)
        {
            UserInfoHelper helper = new UserInfoHelper();
            if (form.NewPassword == form.ConfirmedPassword && form.NewPassword != "")
            {
                helper.UpdatePassword(User.Identity.Name, form);
                return RedirectToAction("/");
            }

            return View("UpdateUserInfo", form);
        }
    }
}